'''
Created on Oct 11, 2012

@author: daeheehan
'''
from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
from pprint import pprint

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"

GITHUB_USER_PW = "qwer1234"

# The criteria date that a developer gets involved in a certain project 
REPO_COMMIT_DURATION_DAYS = 90 

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID_2, password=GITHUB_USER_PW)
print str(gh.ratelimit_remaining) + ' request remaining'

###########################################################################
# Github Bootstrap Repository information
GITHUB_BOOTSTRAP_ORG_NAME = "twitter"
GITHUB_BOOTSTRAP_REPOSITORY_NAME = "bootstrap"

# Bootstrap collaborators information file
GITHUB_BOOTSTRAP_COLLABORATORS_FILE = 'bootstrap_collaborators.txt'

# Bootstrap dedication  information file
GITHUB_BOOTSTRAP_DEDICATION_FILE = 'bootstrap_dedication.txt'

project_collaborator = []
dedication_dict = {}

try:
    # Github Bootstrap project collaborators list from files
    bootstrap_collaborators = open(GITHUB_BOOTSTRAP_COLLABORATORS_FILE, 'r')
    
    for line in bootstrap_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    bootstrap_collaborators.close()
    
    for item in project_collaborator:
        dedication_num = 0
#        item = 'jeremy'
        if item == 'olix0r':
            print str(gh.ratelimit_remaining) + ' request remaining'
            time.sleep(60 * 60)
            
        print 'login id: ' +  item
        repo_list = gh.iter_repos(login=item, type='all', sort='updated')
        if not repo_list:
            print 'no repository list information'
            continue

        for repo in repo_list:
            print repo.html_url, repo.is_collaborator(item), repo.size
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = 1))
                if not repo_commit_list:
                    print 'no commit information'
                    continue

                for repo_commit in repo_commit_list:
                    # Checking what if there is no commit history
                    last_commit_date = repo_commit.commit.author['date']
                    last_date = dateutil.parser.parse(last_commit_date)
#                    print last_date
                    delta = datetime.datetime.now() - last_date.replace(tzinfo=None)
                    if ( delta.days < REPO_COMMIT_DURATION_DAYS ):
                        print delta.days
                        dedication_num = dedication_num + 1

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # the number of dedicated projects by developer(key: login, value: number)            
        dedication_dict[item] = dedication_num
#        break

    bootstrap_dedication = open(GITHUB_BOOTSTRAP_DEDICATION_FILE, 'w')
    print 'Printing dictionary'
    bootstrap_dedication.write('Developer, the number of dedicated projects\n')
    for login_id, projects in dedication_dict.items():
        print 'Developer: ' + str(login_id) + ", The number of projects: " + str(projects)
        bootstrap_dedication.write(str(login_id) + ', ' + str(projects) + '\n')
    
    bootstrap_dedication.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])


print str(gh.ratelimit_remaining) + ' request remaining'

# It's a time to sleep for 1 hour
time.sleep(60 * 60)
###########################################################################
# Github Homebrew Repository information
GITHUB_HOMEBREW_USER_NAME = 'mxcl'
GITHUB_HOMEBREW_REPOSITORY_NAME = 'homebrew'

# Homebrew collaborators information file
GITHUB_HOMEBREW_COLLABORATORS_FILE = 'homebrew_collaborators.txt'

# Homebrew dedication  information file
GITHUB_HOMEBREW_DEDICATION_FILE = 'homebrew_dedication.txt'

project_collaborator = []
dedication_dict = {}

try:
    # Github Homebrew project collaborators list from files
    homebrew_collaborators = open(GITHUB_HOMEBREW_COLLABORATORS_FILE, 'r')
    
    for line in homebrew_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    homebrew_collaborators.close()
    
    for item in project_collaborator:
        dedication_num = 0
#        item = 'jeremy'
        print 'login id: ' +  item
        repo_list = gh.iter_repos(login=item, type='all', sort='updated')
        if not repo_list:
            print 'no repository list information'
            continue

        for repo in repo_list:
            print repo.html_url, repo.is_collaborator(item), repo.size
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = 1))
                if not repo_commit_list:
                    print 'no commit information'
                    continue

                for repo_commit in repo_commit_list:
                    # Checking what if there is no commit history
                    last_commit_date = repo_commit.commit.author['date']
                    last_date = dateutil.parser.parse(last_commit_date)
#                    print last_date
                    delta = datetime.datetime.now() - last_date.replace(tzinfo=None)
                    if ( delta.days < REPO_COMMIT_DURATION_DAYS ):
                        print delta.days
                        dedication_num = dedication_num + 1

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # the number of dedicated projects by developer(key: login, value: number)            
        dedication_dict[item] = dedication_num
#        break

    homebrew_dedication = open(GITHUB_HOMEBREW_DEDICATION_FILE, 'w')
    print 'Printing dictionary'
    homebrew_dedication.write('Developer, the number of dedicated projects\n')
    for login_id, projects in dedication_dict.items():
        print 'Developer: ' + str(login_id) + ", The number of projects: " + str(projects)
        homebrew_dedication.write(str(login_id) + ', ' + str(projects) + '\n')
    
    homebrew_dedication.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'
###########################################################################
# Github Rails Repository information
GITHUB_RAILS_ORG_NAME = 'rails'
GITHUB_RAILS_REPOSITORY_NAME = 'rails'

# Rails collaborators information file
GITHUB_RAILS_COLLABORATORS_FILE = 'rails_collaborators.txt'

# Rails dedication  information file
GITHUB_RAILS_DEDICATION_FILE = 'rails_dedication.txt'

project_collaborator = []
dedication_dict = {}

try:
    # Github Rails project collaborators list from files
    rails_collaborators = open(GITHUB_RAILS_COLLABORATORS_FILE, 'r')
    
    for line in rails_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    rails_collaborators.close()
    
    for item in project_collaborator:
        dedication_num = 0
#        item = 'jeremy'
        print 'login id: ' +  item
        repo_list = gh.iter_repos(login=item, type='all', sort='updated')
        if not repo_list:
            print 'no repository list information'
            continue

        for repo in repo_list:
            print repo.html_url, repo.is_collaborator(item), repo.size
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = 1))
                if not repo_commit_list:
                    print 'no commit information'
                    continue

                for repo_commit in repo_commit_list:
                    # Checking what if there is no commit history
                    last_commit_date = repo_commit.commit.author['date']
                    last_date = dateutil.parser.parse(last_commit_date)
#                    print last_date
                    delta = datetime.datetime.now() - last_date.replace(tzinfo=None)
                    if ( delta.days < REPO_COMMIT_DURATION_DAYS ):
                        print delta.days
                        dedication_num = dedication_num + 1

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # the number of dedicated projects by developer(key: login, value: number)            
        dedication_dict[item] = dedication_num
#        break

    rails_dedication = open(GITHUB_RAILS_DEDICATION_FILE, 'w')
    print 'Printing dictionary'
    rails_dedication.write('Developer, the number of dedicated projects\n')
    for login_id, projects in dedication_dict.items():
        print 'Developer: ' + str(login_id) + ", The number of projects: " + str(projects)
        rails_dedication.write(str(login_id) + ', ' + str(projects) + '\n')
    
    rails_dedication.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'
time.sleep(60 * 60)
###########################################################################
# Github html5 boilerplate Repository information
GITHUB_H5BP_ORG_NAME = 'h5bp'
GITHUB_HTML5_BOILERPLATE_REPOSITORY_NAME = 'html5-boilerplate'

# html5 boilerplate collaborators information file
GITHUB_HTML5_BOILERPLATE_COLLABORATORS_FILE = 'html5_boilerplate_collaborators.txt'

# html5 boilerplate dedication  information file
GITHUB_HTML5_BOILERPLATE_DEDICATION_FILE = 'html5_boilerplate_dedication.txt'

project_collaborator = []
dedication_dict = {}

try:
    # Github html5 boilerplate project collaborators list from files
    html5_boilerplate_collaborators = open(GITHUB_HTML5_BOILERPLATE_COLLABORATORS_FILE, 'r')
    
    for line in html5_boilerplate_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    html5_boilerplate_collaborators.close()
    
    for item in project_collaborator:
        dedication_num = 0
#        item = 'jeremy'
        print 'login id: ' +  item
        repo_list = gh.iter_repos(login=item, type='all', sort='updated')
        if not repo_list:
            print 'no repository list information'
            continue

        for repo in repo_list:
            print repo.html_url, repo.is_collaborator(item), repo.size
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = 1))
                if not repo_commit_list:
                    print 'no commit information'
                    continue

                for repo_commit in repo_commit_list:
                    # Checking what if there is no commit history
                    last_commit_date = repo_commit.commit.author['date']
                    last_date = dateutil.parser.parse(last_commit_date)
#                    print last_date
                    delta = datetime.datetime.now() - last_date.replace(tzinfo=None)
                    if ( delta.days < REPO_COMMIT_DURATION_DAYS ):
                        print delta.days
                        dedication_num = dedication_num + 1

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # the number of dedicated projects by developer(key: login, value: number)            
        dedication_dict[item] = dedication_num
#        break

    html5_boilerplate_dedication = open(GITHUB_HTML5_BOILERPLATE_DEDICATION_FILE, 'w')
    print 'Printing dictionary'
    html5_boilerplate_dedication.write('Developer, the number of dedicated projects\n')
    for login_id, projects in dedication_dict.items():
        print 'Developer: ' + str(login_id) + ", The number of projects: " + str(projects)
        html5_boilerplate_dedication.write(str(login_id) + ', ' + str(projects) + '\n')
    
    html5_boilerplate_dedication.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'
###########################################################################
# Github oh-my-zsh Repository information
GITHUB_ROBBYRUSSELL_ORG_NAME = "robbyrussell"
GITHUB_OH_MY_ZSH_REPOSITORY_NAME = "oh-my-zsh"

# oh-my-zsh collaborators information file
GITHUB_OH_MY_ZSH_COLLABORATORS_FILE = 'oh_my_zsh_collaborators.txt'

# oh-my-zsh dedication  information file
GITHUB_OH_MY_ZSH_DEDICATION_FILE = 'oh_my_zsh_dedication.txt'

project_collaborator = []
dedication_dict = {}

try:
    # Github oh-my-zsh project collaborators list from files
    oh_my_zsh_collaborators = open(GITHUB_OH_MY_ZSH_COLLABORATORS_FILE, 'r')
    
    for line in oh_my_zsh_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    oh_my_zsh_collaborators.close()
    
    for item in project_collaborator:
        dedication_num = 0
#        item = 'jeremy'
        print 'login id: ' +  item
        repo_list = gh.iter_repos(login=item, type='all', sort='updated')
        if not repo_list:
            print 'no repository list information'
            continue

        for repo in repo_list:
            print repo.html_url, repo.is_collaborator(item), repo.size
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = 1))
                if not repo_commit_list:
                    print 'no commit information'
                    continue

                for repo_commit in repo_commit_list:
                    # Checking what if there is no commit history
                    last_commit_date = repo_commit.commit.author['date']
                    last_date = dateutil.parser.parse(last_commit_date)
#                    print last_date
                    delta = datetime.datetime.now() - last_date.replace(tzinfo=None)
                    if ( delta.days < REPO_COMMIT_DURATION_DAYS ):
                        print delta.days
                        dedication_num = dedication_num + 1

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # the number of dedicated projects by developer(key: login, value: number)            
        dedication_dict[item] = dedication_num
#        break

    oh_my_zsh_dedication = open(GITHUB_OH_MY_ZSH_DEDICATION_FILE, 'w')
    print 'Printing dictionary'
    oh_my_zsh_dedication.write('Developer, the number of dedicated projects\n')
    for login_id, projects in dedication_dict.items():
        print 'Developer: ' + str(login_id) + ", The number of projects: " + str(projects)
        oh_my_zsh_dedication.write(str(login_id) + ', ' + str(projects) + '\n')
    
    oh_my_zsh_dedication.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'
time.sleep(60 * 60)
###########################################################################
# Github node Repository information
GITHUB_JOYENT_ORG_NAME = "joyent"
GITHUB_NODE_REPOSITORY_NAME = "node"

# Node collaborators information file
GITHUB_NODE_COLLABORATORS_FILE = 'node_collaborators.txt'

# Node dedication  information file
GITHUB_NODE_DEDICATION_FILE = 'node_dedication.txt'

project_collaborator = []
dedication_dict = {}

try:
    # Github Node project collaborators list from files
    node_collaborators = open(GITHUB_NODE_COLLABORATORS_FILE, 'r')
    
    for line in node_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    node_collaborators.close()
    
    for item in project_collaborator:
        dedication_num = 0
#        item = 'jeremy'
        print 'login id: ' +  item
        repo_list = gh.iter_repos(login=item, type='all', sort='updated')
        if not repo_list:
            print 'no repository list information'
            continue

        for repo in repo_list:
            print repo.html_url, repo.is_collaborator(item), repo.size
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = 1))
                if not repo_commit_list:
                    print 'no commit information'
                    continue

                for repo_commit in repo_commit_list:
                    # Checking what if there is no commit history
                    last_commit_date = repo_commit.commit.author['date']
                    last_date = dateutil.parser.parse(last_commit_date)
#                    print last_date
                    delta = datetime.datetime.now() - last_date.replace(tzinfo=None)
                    if ( delta.days < REPO_COMMIT_DURATION_DAYS ):
                        print delta.days
                        dedication_num = dedication_num + 1

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # the number of dedicated projects by developer(key: login, value: number)            
        dedication_dict[item] = dedication_num
#        break

    node_dedication = open(GITHUB_NODE_DEDICATION_FILE, 'w')
    print 'Printing dictionary'
    node_dedication.write('Developer, the number of dedicated projects\n')
    for login_id, projects in dedication_dict.items():
        print 'Developer: ' + str(login_id) + ", The number of projects: " + str(projects)
        node_dedication.write(str(login_id) + ', ' + str(projects) + '\n')
    
    node_dedication.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'

###########################################################################
# Github jquery Repository information
GITHUB_JQUERY_ORG_NAME = "jquery"
GITHUB_JQUERY_REPOSITORY_NAME = "jquery"

# jquery collaborators information file
GITHUB_JQUERY_COLLABORATORS_FILE = 'jquery_collaborators.txt'

# jquery dedication  information file
GITHUB_JQUERY_DEDICATION_FILE = 'jquery_dedication.txt'

project_collaborator = []
dedication_dict = {}

try:
    # Github jquery project collaborators list from files
    jquery_collaborators = open(GITHUB_JQUERY_COLLABORATORS_FILE, 'r')
    
    for line in jquery_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    jquery_collaborators.close()
    
    for item in project_collaborator:
        dedication_num = 0
#        item = 'jeremy'
        print 'login id: ' +  item
        repo_list = gh.iter_repos(login=item, type='all', sort='updated')
        if not repo_list:
            print 'no repository list information'
            continue

        for repo in repo_list:
            print repo.html_url, repo.is_collaborator(item), repo.size
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = 1))
                if not repo_commit_list:
                    print 'no commit information'
                    continue

                for repo_commit in repo_commit_list:
                    # Checking what if there is no commit history
                    last_commit_date = repo_commit.commit.author['date']
                    last_date = dateutil.parser.parse(last_commit_date)
#                    print last_date
                    delta = datetime.datetime.now() - last_date.replace(tzinfo=None)
                    if ( delta.days < REPO_COMMIT_DURATION_DAYS ):
                        print delta.days
                        dedication_num = dedication_num + 1

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # the number of dedicated projects by developer(key: login, value: number)            
        dedication_dict[item] = dedication_num
#        break

    jquery_dedication = open(GITHUB_JQUERY_DEDICATION_FILE, 'w')
    print 'Printing dictionary'
    jquery_dedication.write('Developer, the number of dedicated projects\n')
    for login_id, projects in dedication_dict.items():
        print 'Developer: ' + str(login_id) + ", The number of projects: " + str(projects)
        jquery_dedication.write(str(login_id) + ', ' + str(projects) + '\n')
    
    jquery_dedication.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

###########################################################################

print str(gh.ratelimit_remaining) + ' request remaining'

print
print 'Finally the end'
